package cn.jilian.admin.tool.service.support;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jilian.admin.tool.service.support.formatters.DateFormatter;
import cn.jilian.admin.tool.service.support.formatters.NumberFormatter;
import cn.jilian.admin.tool.service.support.formatters.StringFormatter;

public class FormatterFactory {
	
	public static Map<String,Formatter> TYPE_FORMATTER_MAP=new HashMap<>();
	static {
		TYPE_FORMATTER_MAP.put("varchar",  StringFormatter.instance);
		TYPE_FORMATTER_MAP.put("char",  StringFormatter.instance);
		TYPE_FORMATTER_MAP.put("text",  StringFormatter.instance);
		TYPE_FORMATTER_MAP.put("mediumntext",  StringFormatter.instance);
		TYPE_FORMATTER_MAP.put("longtext",  StringFormatter.instance);
		TYPE_FORMATTER_MAP.put("int",  NumberFormatter.instance);
		TYPE_FORMATTER_MAP.put("long",  NumberFormatter.instance);
		TYPE_FORMATTER_MAP.put("bigint",  NumberFormatter.instance);
		TYPE_FORMATTER_MAP.put("decimal",  NumberFormatter.instance);
		TYPE_FORMATTER_MAP.put("float",  NumberFormatter.instance);
		TYPE_FORMATTER_MAP.put("double",  NumberFormatter.instance);
		TYPE_FORMATTER_MAP.put("smallint",  NumberFormatter.instance);
		TYPE_FORMATTER_MAP.put("bit",  NumberFormatter.instance);
		TYPE_FORMATTER_MAP.put("smallint unsigned",  NumberFormatter.instance);
		TYPE_FORMATTER_MAP.put("timestamp",  DateFormatter.TSFORMATTER);
		TYPE_FORMATTER_MAP.put("time",  DateFormatter.TIMEFORMATTER);
		TYPE_FORMATTER_MAP.put("datetime",  DateFormatter.DTFORMATTER);
	}
	
	public static Map<String,Formatter> create(List<Map<String,Object>> fields){
		Map<String,Formatter> fm=new HashMap<>();
		for(Map<String,Object> f:fields) {
			String type=(String) f.get("DATA_TYPE");
			fm.put((String)f.get("COLUMN_NAME"), TYPE_FORMATTER_MAP.get(type));
		}
		return fm;
	}

}
